package com.lgl.scsheader.sameCityChart.service;

import com.lgl.scsapi.base.dao.DaoSupport;
import com.lgl.scsapi.system.model.PageData;
import com.lgl.scsapi.util.utils.Tools;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 订单城市图表service
 */

@Service("userOrderChartService")
public class UserOrderChartService {
	@Resource(name = "daoSupport")
	private DaoSupport dao;

	/**
	 * 获取每个月有多少用户下单/抢单
	 * @param thisLastPd 统计参数，如统计的某年年份
	 * @return 本年与上一年每个月有多少用户下单/抢单
	 */
	public List<PageData> listMonthUserTotal(PageData thisLastPd){
		if (thisLastPd == null){
			thisLastPd = new PageData();
		}
		int thisYear = Tools.getYear(thisLastPd);
		StringBuffer issueCountSql = new StringBuffer(), robCountSql = new StringBuffer();
		//拼接12个月的统计SQL
		for (int g = 1; g < 13; g ++){
			String m = g < 10 ? "0" : "";
			issueCountSql.append("COUNT(IF(right(i.orderDate, 2) = '" + m + g + "', 1, NULL)) as '" + g + "',");
			robCountSql.append("COUNT(IF(right(i.robDate, 2) = '" + m + g + "', 1, NULL)) as '" + g + "',");
		}
		int lastYear = thisYear - 1;
		thisLastPd.put("lastYear",lastYear);
		thisLastPd.put("issueMonthSql",issueCountSql);
		thisLastPd.put("robMonthSql",robCountSql);
		Tools.initDate(thisLastPd,thisYear,lastYear);
		return (List<PageData>) dao.findForList("ServiceOrderMapper.listMonthUserTotal", thisLastPd);
	}
	/**
	 * 获取从未下过单、未抢过单、抢过单未下过单、下过单未抢过单、既下单又抢单、下过单、抢过单等情况下的用户量
	 * @param issueRobPd 统计参数
	 * @return 用户各情况下的数量
	 */
	public List<PageData> listIssueRobUserTotal(PageData issueRobPd){
		return (List<PageData>) dao.findForList("ServiceOrderMapper.listIssueRobUserTotal", issueRobPd);
	}
    /**
     * 获取2年内发布和抢单最多的6个用户
     * @param issueRobPd 统计参数
     * @return 2年内发布和抢单最多的6个用户
     */
    public List<PageData> listIssueRobMostUser(PageData issueRobPd){
        int thisYear = Tools.getYear(issueRobPd);
        issueRobPd.put("thisYear",thisYear);
        Tools.initDate(issueRobPd,thisYear,thisYear);
        return (List<PageData>) dao.findForList("ServiceOrderMapper.listIssueRobMostUser", issueRobPd);
    }
    /**
     * 获取2年内周几发布和抢单用户数
     * @param weekPd 统计参数，如年份
     * @return 2年内周几发布和抢单用户数
     */
    public List<PageData> listIssueRobWeekUser(PageData weekPd){
        int thisYear = Tools.getYear(weekPd);
        StringBuilder weekSql = new StringBuilder("COUNT(IF(myWeek = '1', 1, NULL)) as '7',");
        for (int g = 1; g < 7; g ++){
            weekSql.append("COUNT(IF(myWeek = '" + (g + 1) + "', 1, NULL)) as '" + g + "',");
        }
        weekPd.put("countWeek", weekSql.toString());
        Tools.initDate(weekPd,thisYear,thisYear - 1);
        return (List<PageData>) dao.findForList("ServiceOrderMapper.listIssueRobWeekUser", weekPd);
    }
    /**
     * 获取每个月下单/抢单男女用户的数量
     * @param sexPd 统计参数，如年份
     * @return 每个月下单/抢单男女用户的数量
     */
    public List<PageData> listMonthUserSexTotal(PageData sexPd){
        int thisYear = Tools.getYear(sexPd);
        Tools.initDate(sexPd,thisYear,thisYear);
        return (List<PageData>) dao.findForList("ServiceOrderMapper.listMonthUserSexTotal", sexPd);
    }
    /**
     * 获取下单/抢单用户70前、70后、80后、90后、00后的数量
     * @param ageSectionPd 统计参数，如年份
     * @return 下单/抢单用户70前、70后、80后、90后、00后的数量
     */
    public List<PageData> listIssueRobAgeSection(PageData ageSectionPd){
        int thisYear = Tools.getYear(ageSectionPd);
        Tools.initDate(ageSectionPd,thisYear,thisYear);
        return (List<PageData>) dao.findForList("ServiceOrderMapper.listIssueRobAgeSection", ageSectionPd);
    }
}